package io.github.talelin.latticy.controller.v1;

import io.github.talelin.core.annotation.*;
import io.github.talelin.latticy.dto.userware.CreateUserwareDTO;
import io.github.talelin.latticy.dto.userware.UpdateUserwareDTO;
import io.github.talelin.latticy.service.UserwareService;
import io.github.talelin.latticy.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.UserwareDO;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;
import java.util.List;

/**
 * @author generator@wulongbo
 * @since 2022-05-12
 */
@RestController
@RequestMapping("/v1/userware")
@PermissionModule(value = "用户仓库")
@Validated
public class UserwareController {

    @Autowired
    private UserwareService userwareService;

    @AdminRequired
    @PostMapping("")
    @PermissionMeta(value = "用户绑定仓库", mount = false)
    public CreatedVO create(@RequestBody @Valid CreateUserwareDTO createUserwareDTO) {
        userwareService.createUserware(createUserwareDTO);
        return new CreatedVO();
    }

    @AdminRequired
    @PermissionMeta(value = "用户修改仓库", mount = false)
    @PutMapping("")
    public UpdatedVO update(@RequestBody @Validated UpdateUserwareDTO updateUserwareDTO) {
        userwareService.updateUserware(updateUserwareDTO);
        return new UpdatedVO();
    }

//    @DeleteMapping("/{id}")
//    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
//        return new DeletedVO();
//    }

    @AdminRequired
    @GetMapping("/{id}")
    @PermissionMeta(value = "超级管理员查询分组用户关联的仓库列表", mount = false)
    public List<WarehouseVO> getWarehouseById(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return userwareService.getWareNameListByUserId(id);
    }

    @LoginRequired
    @GetMapping("")
    public List<WarehouseVO> getWarehouse() {
        return userwareService.getWareNameList();
    }

    @GetMapping("/page")
    public PageResponseVO<UserwareDO> page(
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count
    ) {
        return null;
    }

}
